//
//  File.swift
//  VVeibo21
//
//  Created by 李浩杰 on 16/10/22.
//  Copyright © 2016年 李浩杰. All rights reserved.
//

import UIKit

extension UIButton {
    
    /// 创建图片按钮并且带背景图片
    ///
    /// - parameter setImgName:       图片名字
    /// - parameter bacgroundImgName: 背景图片名字
    /// - parameter target:           所有者
    /// - parameter action:           监听方法名
    ///
    /// - returns: 
    convenience init (setImgName: String, bacgroundImgName: String, target: Any?, action: Selector ) {
        self.init()
        //添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        //设置image
        self.setImage(UIImage(named: setImgName), for: UIControlState.normal)
        self.setImage(UIImage(named: "\(setImgName)_highlighted"), for: UIControlState.highlighted)
        //设置背景图片
        self.setBackgroundImage(UIImage(named: bacgroundImgName), for: UIControlState.normal)
        self.setBackgroundImage(UIImage(named: "\(bacgroundImgName)_highlighted"), for: UIControlState.highlighted)
        self.sizeToFit()
    }
    
    /// 创建图片和标题可为nil的按钮
    ///
    /// - parameter setHighlightedImgName: 图片
    /// - parameter title:                 标题
    /// - parameter target:                所有者
    /// - parameter action:                监听方法名
    ///
    /// - returns:
    convenience init(setHighlightedImgName: String? = nil, title:  String? = nil, target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        //可为nil背景图片，:该方法可以不传背景图片
        if let img = setHighlightedImgName {
            self.setImage(UIImage(named: img), for: UIControlState.normal)
            self.setImage(UIImage(named: "\(img)_highlighted"), for: UIControlState.highlighted)
        }
        if let tit = title {
            self.setTitle(tit, for: UIControlState.normal)
            self.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            self.setTitleColor(HMTHEMECOLOR, for: UIControlState.highlighted)
            self.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        }
        self.sizeToFit()
    }
}
